﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

namespace DeansWsccProject
{
    public partial class edit_profile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //displayDataFromTable("SELECT FirstName, LastName, Description FROM profile WHERE aspnetUserName = @f1", User.Identity.Name, tbFirstName.Text, tbLastName.Text, tbDescription.Text");
                OleDbConnection oleDbConn = getDbConnection();
                oleDbConn.Open();
                OleDbCommand cmd = new OleDbCommand("SELECT * FROM profile WHERE profileUserName = @f1", oleDbConn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@f1", User.Identity.Name);
                OleDbDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    int firstNameValue = dataReader.GetOrdinal("FirstName");
                    tbFirstName.Text = Convert.ToString(dataReader.GetValue(firstNameValue));
                    int lastNameValue = dataReader.GetOrdinal("LastName");
                    tbLastName.Text = Convert.ToString(dataReader.GetValue(lastNameValue));
                    int descriptionValue = dataReader.GetOrdinal("Description");
                    tbDescription.Text = Convert.ToString(dataReader.GetValue(descriptionValue));
                }
                cmd.Dispose();
                //dataReader.Close();
                dataReader.Dispose();
                //oleDbConn.Close();
                oleDbConn.Dispose();
            }
        }

        protected void btnUpdateProfile_Click(object sender, EventArgs e)
        {
            // Upload profile jpg file if it is submitted, and set sql string for updating profile table accordingly
            string newProfileJpgFilename = getUniqueFilename("jpg");
            if (fuPicture.HasFile)
            {
                fuPicture.SaveAs(Request.MapPath("ProfileImages") + "/" + newProfileJpgFilename);
            }

            OleDbConnection oleDbConn = getDbConnection();
            oleDbConn.Open();

            // Update profile table
            OleDbCommand cmdUpdateProfile = new OleDbCommand("UPDATE profile SET FirstName=@f1,LastName=@f2,Description=@f3,Picture=@f4 WHERE profileUserName=@f5", oleDbConn);
            cmdUpdateProfile.CommandType = CommandType.Text;
            cmdUpdateProfile.Parameters.AddWithValue("@f1", tbFirstName.Text);
            cmdUpdateProfile.Parameters.AddWithValue("@f2", tbLastName.Text);
            cmdUpdateProfile.Parameters.AddWithValue("@f3", tbDescription.Text);
            if (fuPicture.HasFile) { cmdUpdateProfile.Parameters.AddWithValue("@f4", "ProfileImages/" + newProfileJpgFilename); }
            else { cmdUpdateProfile.Parameters.AddWithValue("@f4", "ProfileImages/default.jpg"); }
            cmdUpdateProfile.Parameters.AddWithValue("@f5", User.Identity.Name);
            cmdUpdateProfile.ExecuteNonQuery();
            cmdUpdateProfile.Dispose();
            oleDbConn.Close();
            oleDbConn.Dispose();
            Response.Redirect("~/profile.aspx?profileUserName=" + User.Identity.Name);
        }

        private string getUniqueFilename(string fileExtension)
        {
            System.Guid myGUID = new System.Guid();
            myGUID = Guid.NewGuid();
            return myGUID.ToString() + "." + fileExtension;
        }

//        private void displayDataFromTable(string sqlString, string aspIdsStr)
//        {
//            //string selectWhat = Regex.Replace(sqlString, "SELECT(.*)FROM.*"); //{ "SELECT", "FROM" },
//            char[] charSeparators = new char[] {',', ' '};
//            string[] sqlSelects = (sqlString.Replace("SELECT", "")).Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
//            //string[] selectWhat = selectWhatStr.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
//            string[] aspIds = aspIdsStr.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
//            OleDbConnection oleDbConn = getDbConnection();
//            oleDbConn.Open();
//            OleDbCommand cmdDisplayDataFromTable = new OleDbCommand(sqlString, oleDbConn);
//            cmdDisplayDataFromTable.CommandType = CommandType.Text;
//            //cmdGetDataFromWhere.Parameters.AddWithValue("@f1", equalsKnownValue);
//            OleDbDataReader dataReader = cmdDisplayDataFromTable.ExecuteReader();
//            //List<string> dataList = new List<string>();
//            while (dataReader.Read())
//            {
//                for (int i = 0; i < aspIds.Length; i++)
//                {
//                    int value = dataReader.GetOrdinal(sqlSelects[i]);
//                    aspIds[i] = Convert.ToString(dataReader.GetValue(value));
//                }
//            }
//            cmdDisplayDataFromTable.Dispose();
//            dataReader.Close();
//            dataReader.Dispose();
//            oleDbConn.Close();
//            oleDbConn.Dispose();
//        }

        private OleDbConnection getDbConnection()
        {
            string connectionStr = getConnectionString();
            OleDbConnection oleDbConn = new OleDbConnection(ConfigurationManager.ConnectionStrings[connectionStr].ConnectionString);
            return oleDbConn;
        }
        private string getConnectionString()
        {
            return "DeansWsccProjectConnectionString";
        }
    }
}